<?php
	include_once "include/params.php";

	$params = array(
		'table'		=> $GLOBALS['table_view_main'],
		'start'		=> 0,
		'limit'		=> $GLOBALS['table_view_size']
	);

?>
<h1>Tabella <b id="tableName"><?php echo $params['table']; ?></b></h1>
<div id="submenu">
	<a id="sfull" href="javascript:showFull();">mostra tutto</a>&nbsp;&nbsp;
	<i id="pages">
		<a href="javascript:scrollPage(-1);">&lt;&lt;--</a>
		<b id="curPage">1</b>/<b id="totPag">1</b>
		<a href="javascript:scrollPage(+1);">--&gt;&gt;</a>
	</i>
</div>
<div id="filter">
</div>
<br>
<div id="table">
</div>

<script language="javascript" type="text/javascript">
	function showTable(name,offset,size)
	{
		//aggiorna il titolo
		tableName.innerHTML = name;
		//inoltra richiesta
		req.loadIntoHTML(grid, "aj_table_show.php", {
			table:name,
			start:offset,
			limit:size});
	}
	function showFull()
	{
		if(sfull.innerHTML == "mostra parziale")
		{
			sfull.innerHTML = "mostra tutto";
			pages.style.display = "";
			tableName.innerHTML = db_table;
			showTable(db_table, page_number, page_size);
		}
		else if(sfull.innerHTML == "mostra tutto")
		{
			pages.style.display = "none";
			tableName.innerHTML = db_table+" (full)";
			sfull.innerHTML = "mostra parziale";
			req.loadIntoHTML(grid, "aj_table_show.php", {table:db_table,fn:"full"});
		}
	}
	function tableChange(name)
	{
		db_table = name;
		req.loadIntoVar("aj_table_show.php", {
			table:name,	fn:"size"}, tableChange_size);
	}
	function tableChange_size(value)
	{
		if(value=="fail") return;

		// resetta le variabili di navigazione
		page_number = <?php echo $params['start'] ?>;
		page_size = <?php echo $params['limit'] ?>;

		db_rows = value;
		cur.innerHTML = page_number;
		tot.innerHTML = Math.floor(value/page_size);
		pages.style.display = "";

		showTable(db_table, page_number, page_size);
	}
	function scrollPage(n)
	{
		if((page_number+n*page_size)>db_rows || (page_number+n*page_size)<0)
			return;
		page_number += n*page_size;
		cur.innerHTML = page_number/page_size;
		showTable(db_table, page_number, page_size);
	}

	// oggetti
	var grid = dom_getObj('table');
	var sfull = dom_getObj('sfull');
	var tableName = dom_getObj('tableName');
	var tot = dom_getObj('totPag');
	var cur = dom_getObj('curPage');
	var pages = dom_getObj('pages');
	var req = new AJRequest();
	// variabili di navigazione pagine
	var page_number;
	var page_size;
	// variabili di db
	var db_rows;
	var db_table;

	// visualizza la prima tabella di default o da parametro
	tableChange('<?php echo $params['table'] ?>');
</script>
